package com.ruoyi.exam.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.exam.domain.PQuestionTypeEntity;
import com.ruoyi.exam.mapper.PQuestionMapper;
import com.ruoyi.exam.mapper.PQuestionTypeEntityMapper;
import com.ruoyi.exam.service.PQuestionTypeService;
import com.ruoyi.exam.vo.PQuestionTypeEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author coldplay
 * @create 2023-10-25 9:09
 */
@Service
public class PQuestionTypeServiceImpl implements PQuestionTypeService {

  @Autowired
  PQuestionTypeEntityMapper pQuestionTypeEntityMapper;

  @Autowired
  PQuestionMapper pQuestionMapper;


  @Override
  public List<PQuestionTypeEntity> selectAll() {
    return pQuestionTypeEntityMapper.selectAll();
  }

  @Override
  public AjaxResult updateByNames() {

    return AjaxResult.success();
  }

  @Override
  public AjaxResult updateByIds(List<PQuestionTypeEntityVo> typeEntityVos) {

    typeEntityVos.forEach(
      typeEntityVo->
      pQuestionTypeEntityMapper
        .updateByPrimaryKey(
          new PQuestionTypeEntity(
          typeEntityVo.getId(),
          typeEntityVo.getName()
          )
        )
    );
    return AjaxResult.success();
  }

  @Override
  public AjaxResult updateById(PQuestionTypeEntityVo typeEntityVo) {
    pQuestionTypeEntityMapper.updateByPrimaryKey(new PQuestionTypeEntity(typeEntityVo.getId(), typeEntityVo.getName()));
    return AjaxResult.success();
  }

  @Override
  public AjaxResult deleteById(Integer id) {
    int i = pQuestionMapper.selectCountQuestionByType(id);
    if(i > 0) return AjaxResult.error("题库中还有该类型的题目，删除失败");
    pQuestionTypeEntityMapper.deleteByPrimaryKey(id);
    return AjaxResult.success();
  }

  @Override
  public AjaxResult addType(String name,Integer type) {
    pQuestionTypeEntityMapper.insert(name,type);
    return AjaxResult.success();
  }

  @Override
  public AjaxResult getById(Integer id) {
    PQuestionTypeEntity pQuestionTypeEntity = pQuestionTypeEntityMapper.selectByPrimaryKey(id);
    return AjaxResult.success(pQuestionTypeEntity);
  }
}
